package pkg.hicph.org;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class SleepScreen extends Activity implements OnClickListener  {
    
    public TextView sleepInfo, sleepTitle;
    public Button actionButton, cancelButton;


	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  
	  requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
	  
	  setContentView(R.layout.sleepscreen);
	  
	  registerViews();
	  setupOnClickListeners();
	  
//	  this.setTitle("You just found this place:");
	  
	  if(Utils.gamePhase == Utils.PLAY_SLEEP_MODE){
		  sleepTitle.setText("Are you sure you will sleep here?");
		  sleepInfo.setText("Remember, you need to be safe to spend the night. If you sleep somewhere too exposed or busy, the chances of something bad happening get much higher.");
		  cancelButton.setVisibility(0);
	  }
	  else {
		  sleepTitle.setText("After eating, you feel really tired!");
		  sleepInfo.setText("You had some food and now you feel really tired. This area is far from the shelters you would go, and you don't want to go too far. You need to find a good, safe place to sleep around here tonight.");
	  }

	  
	}
	
	
//	private void loadPrefs() {
//		
//		String actionName;
//		
//    	preferences  = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
//    	
//    	actionName = preferences.getString("nextActionName", "");
//    	
//    	Log.d("DIALOG","loaded: " + actionName);
//    	
//    	actionToPerform = Utils.nextAction;
//    	
//    	actionToPerform = Utils.getActionByName(actionName);
//    	Log.d("DIALOG","action: " + actionToPerform.name);
//        
//	}
	
	
	private void registerViews() {
			
		 actionButton = (Button) this.findViewById(R.id.sleep_action_button);
		 cancelButton = (Button) this.findViewById(R.id.sleep_cancel_button);
		 sleepInfo = (TextView) this.findViewById(R.id.sleep_dialog_info);
		 sleepTitle = (TextView) this.findViewById(R.id.sleep_dialog_title);
	}

	
	public void setupOnClickListeners() {
        
		actionButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);

    }
    
	
    public void onClick(View V) {
    	
    	switch(V.getId()) {
    	
    	case R.id.sleep_action_button:
    		
    		// COMPUTES ACTION
    		
    		if(Utils.gamePhase == Utils.PLAY_SLEEP_MODE){
    			
    			Utils.gamePhase = Utils.END_SLEEP_MODE;
    			
    			
    			if(Utils.sleepSafety>70){
    				Utils.player.dayStory += "\n\nYou found a quite safe place and slept there.";
    				
    				if(!Utils.getEventByName("Safe sleeper").achieved){
    					Utils.getEventByName("Safe sleeper").achieved = true;
    					Utils.achievedInSession++;
    				}
    			}
    			else if(Utils.sleepSafety>30){
    				Utils.player.dayStory += "\n\nYou couldn't find a good place, but you had to sleep.";
    			}
    			else {
    				Utils.player.dayStory += "\n\nYou settled in a really risky place to sleep!";
    				
    				if(!Utils.getEventByName("Risky night").achieved){
    					Utils.getEventByName("Risky night").achieved = true;
    					Utils.achievedInSession++;
    				}
    				
    			}
    			
    			
    		}
    		else {
    		
    			Utils.gamePhase = Utils.START_SLEEP_MODE;
    		}
    		
    		// CLOSES THE DIALOG
    		this.finish();
    		
    		break;
    		
    	case R.id.sleep_cancel_button:
    		
    		// COMPUTES ACTION
    		
    		if(Utils.gamePhase == Utils.PLAY_SLEEP_MODE){
    			
    			
    		}
    		else {
    		
    			
    		}
    		
    		
    		
    		// CLOSES THE DIALOG
    		this.finish();
    		
    		break;
    		
    		
    	}

    }


}




